package cn.iocoder.yudao.module.member.service.cardsecurity;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.member.controller.app.cardsecurity.vo.ExchangeConfigRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.cardsecurity.ExchangeConfigDO;
import cn.iocoder.yudao.module.member.dal.mysql.cardsecurity.ExchangeConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

@Service
@Validated
public class ExchangeConfigServiceImpl implements ExchangeConfigService {

    @Resource
    private FileApi fileApi;
    //常用二级域名
    private static final String DEFAULT_EXCHANGE = "edu,net,com,org,gov,mil,biz,info,www,qq,app";
    //默认绑定域名
    private static final String DEFAULT_BING = "https://www.yuntengjuhe.com";
    @Resource
    private ExchangeConfigMapper exchangeConfigMapper;

    /**
     * 创建兑换站点配置
     *
     * @param createReqVO 创建信息
     * @return 编号
     */
    @Override
    public Long createConfig(ExchangeConfigRespVO createReqVO) {
        Long userId = getLoginUserId();
        List<String> split = StrUtil.split(DEFAULT_EXCHANGE, ",");
        if (split.contains(createReqVO.getPrefix())) {
            throw exception(PREFIX_ERROR);
        }
        createReqVO.setBindDomainName(DEFAULT_BING);
        //校验分销前缀 不能重复
        ExchangeConfigDO exchangeConfigDOByDb = exchangeConfigMapper.selectByprefix(createReqVO.getPrefix(), createReqVO.getId());
        if (exchangeConfigDOByDb != null) {
//            throw new RuntimeException("分销前缀不能重复");
            throw exception(PREFIX_ERROR_REPEAT);
        }
        if (exchangeConfigMapper.selectById(createReqVO.getId()) == null) {
            ExchangeConfigDO config = BeanUtils.toBean(createReqVO, ExchangeConfigDO.class);
            config.setUserId(userId);
            exchangeConfigMapper.insert(config);
            moveFile(config);
            return config.getId();
        } else {
            ExchangeConfigDO updateObj = BeanUtils.toBean(createReqVO, ExchangeConfigDO.class);
            exchangeConfigMapper.updateById(updateObj);
            moveFile(updateObj);
            return updateObj.getId();
        }
    }

    private void moveFile(ExchangeConfigDO configDO) {
        if (StrUtil.isNotBlank(configDO.getBackgroundImage())) {
            fileApi.moveFile(CharSequenceUtil.subAfter(configDO.getBackgroundImage(), "/get/", false), "icon");
        }
        if (StrUtil.isNotBlank(configDO.getSiteIcon())) {
            fileApi.moveFile(CharSequenceUtil.subAfter(configDO.getSiteIcon(), "/get/", false), "icon");
        }
    }

    /**
     * 获得兑换站点配置
     *
     * @return 兑换站点配置
     */
    @Override
    public ExchangeConfigDO getConfig() {
        Long userId = getLoginUserId();
        return exchangeConfigMapper.selectByUserId(userId);
    }

    @Override
    public ExchangeConfigDO getByPrefix(String prefix) {
        return exchangeConfigMapper.getByPrefix(prefix);
    }
}
